package com.kunbo.controller.local;

import com.kunbo.entity.Event;
import com.kunbo.entity.Page;
import com.kunbo.entity.User;
import com.kunbo.event.EventProducer;
import com.kunbo.service.FollowService;
import com.kunbo.service.UserService;
import com.kunbo.util.CommunityConstant;
import com.kunbo.util.HostHolder;
import com.kunbo.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements CommunityConstant{

    @Autowired
    private FollowService followService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private EventProducer eventProducer;

    //关注某人
    @RequestMapping(value = "/follow", method = RequestMethod.POST)
    @ResponseBody
    public String follow(String entityId){
        int entityType = 1;
        User user = hostHolder.getUser();
        User toUser = userService.findUserByName(entityId);
        followService.follow(user.getId(), entityType, toUser.getId());

        //触发关注事件
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(user.getId());
        eventProducer.fireEvent(event);

        return JSON.getJSONString("0", "已关注");
    }

    //取关某人
    @RequestMapping(value = "/unfollow", method = RequestMethod.POST)
    @ResponseBody
    public String unFollow(int entityType, int entityId){
        User user = hostHolder.getUser();

        followService.unFollow(user.getId(), entityType, entityId);

        return JSON.getJSONString("0", "已取消关注");
    }

    //查看所有跟随者
    @RequestMapping(value = "/followees/{userId}", method = RequestMethod.GET)
    public String getFollowees(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.findUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/followees/" + userId);
        page.setRows((int)followService.findFolloweeCount(userId, CONSUMER));

        List<Map<String, Object>> userList = followService.findFollows(userId, page.getOffers(), page.getLimit());
        if(userList != null){
            for(Map<String, Object> map : userList){
                User u = (User)map.get("user");
                map.put("hasFollowed", hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users", userList);

        return "site/followlist";
    }

    private boolean hasFollowed(int userId){
        if(hostHolder.getUser() == null){
            return false;
        }

        return followService.hasFollowed(hostHolder.getUser().getId(), CONSUMER, userId);
    }

}
